<?php
// +----------------------------------------------------------------------
// | 会员卡数据处理
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/03/06
// +----------------------------------------------------------------------
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/module/pft_show/PftShow.class.php';
include_once '/var/www/html/new/d/module/common/Db.class.php';
$dbConf = include '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息
use \PFT\Db;
Db::Conf($dbConf['remote_1']);


if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');




switch(trim($_REQUEST['action'])) {
	
	
	// 场馆、分区数据处理
	case 'venues':
	
		$recvData = _addslashes($_REQUEST);// 接收数据
		// print_r($recvData);
		// exit;
		$venue_id = $recvData['venue_id'] = $recvData['venue_id'] + 0;
		if($recvData['venue_id']==0){// 新增场馆
			if($recvData['venue_name']=='') exit('{"status":"fail","msg":"场馆名称不能为空"}');
			
			// 名称唯一性
			$sql = "select id from pft_venues where venue_name='{$recvData['venue_name']}' and apply_did={$_SESSION['sid']} and status=0 limit 1";
			if(Db::fetch($sql)) exit('{"status":"fail","msg":"场馆名称已存在"}');
			
			$save['status']      = 0;
			$save['apply_did']   = $_SESSION['sid'];
			$save['venue_name']  = $recvData['venue_name'];
			$save['venue_thumb'] = $recvData['venue_thumb'];
			$sql = _insertSqlBuild($save, 'pft_venues');
			$venue_id = (Db::execute($sql)) ? Db::lastInsertId():0;
		}else{// 更新
			$sql = "select id from pft_venues where venue_name='{$recvData['venue_name']}' order by id desc limit 1";
			$dbRow = DB::fetch($sql);
			if($dbRow && $dbRow['id']!=$venue_id)exit('{"status":"fail","msg":"场馆名称已存在"}');
			$sql = "update pft_venues set venue_name='{$recvData['venue_name']}',venue_thumb='{$recvData['venue_thumb']}' where id=$venue_id limit 1";
			Db::execute($sql);
		}

		if($venue_id==0) exit('{"status":"fail","msg":"参数出错"}');
		
		// 分区操作
		if(isset($recvData['area_id']) && count($recvData['area_id'])){
			foreach($recvData['area_id'] as $key=>$area_id){
				
				$area_name  = $recvData['area_name'][$key];
				$isDelete   = $recvData['isDelete'][$key];// 1  添加 0 删除

				if($area_id>0){// 存在数据
					if($isDelete==0){// 删除
						$sql = "delete from pft_roundzone where id=$area_id and venue_id=$venue_id limit 1";
						Db::execute($sql);continue;
					}
					$sql = "update pft_roundzone set zone_name='$area_name' where id=$area_id and venue_id=$venue_id limit 1";
					Db::execute($sql);continue;
				}
				
				// 新增分区
				$sql = "select id from pft_roundzone where venue_id=$venue_id and zone_name='$area_name' order by id desc limit 1";
				if(!Db::fetch($sql)){
					$ins = "insert into pft_roundzone set venue_id=$venue_id,zone_name='$area_name'";
					Db::execute($ins);
				}
			}
		}
		
		exit('{"status":"success","msg":"保存成功","venue_id":'.$venue_id.'}');
		break;
	// 分区生成座位信息
	case 'set_seat':
		$recvData = json_decode($_REQUEST['data'], true);
		$venue_id    = $recvData['venue_id'] + 0;
		$seat_row    = $recvData['row_num'] + 0;
		$seat_col    = $recvData['col_num'] + 0;
		$seat_layout = $recvData['layout'] + 0;
		$col_side    = $recvData['row_head'];
		$col_head    = $recvData['col_head'];
		$sql = "update pft_venues set row_num=$seat_row,col_num=$seat_col,layout=$seat_layout,col_side='$col_side',col_head='$col_head' where id=$venue_id limit 1";
		if(!Db::execute($sql)) exit('{"status":"fail","msg":"场馆信息更新失败"}');

		// 获取场馆座位信息
		$sql = "select id,row_num,col_num,row_id,col_id,custom_num,seat_status,zone_id from pft_roundseat where venue_id=$venue_id";
		$dbRows = Db::fetchAll($sql);
		foreach($dbRows as $key=>$row){
			$row_col = $row['row_num'].'-'.$row['col_num'];
			$dbSetArr[$row_col] = $row;
		}
		// print_r($dbSetArr);
		$seat_del_id = array();
		// 外层分区
		$ins   = "insert into pft_roundseat (venue_id,zone_id,custom_num,col_num,row_num,col_id,row_id,seat_status) values ";
		$ins_f = false;
		foreach($recvData['areas'] as $area_id=>$area){
			
			// $sql = "select rd.id from pft_roundseat_dyn rd left join pft_round r on rd.round_id=r.id ";
			// $sql.= "where rd.zone_id=$area_id and rd.ordernum<>'' and r.use_date>='".date('Y-m-d')."' limit 1";
			// if(Db::fetch($sql)){
				// foreach($area['seats'] as $k=>$seat){
					// $row_col = $seat['row_num'].'-'.$seat['col_num'];
					// $seat_id = $dbSetArr[$row_col]['id'];
					// $seat_del_id[] = $seat_id;
				// }
				// break;
			// }
			
			foreach($area['seats'] as $k=>$seat){
				$area_id_inside = $area['zone_id'];
				$custom_num = $seat['seat'];
				$row_col = $seat['row_num'].'-'.$seat['col_num'];
				if(isset($dbSetArr[$row_col])){// 存在相同座位号
					$seat_id = $dbSetArr[$row_col]['id'];
					$seat_del_id[] = $seat_id;
					if($seat['status']==$dbSetArr[$row_col]['seat_status'] && $seat['seat']==$dbSetArr[$row_col]['custom_num']
						 && $seat['row_id']==$dbSetArr[$row_col]['row_id'] && $seat['col_id']==$dbSetArr[$row_col]['col_id']
						   && $seat['row_num']==$dbSetArr[$row_col]['row_num'] && $seat['col_num']==$dbSetArr[$row_col]['col_num']
						    && $dbSetArr[$row_col]['zone_id']==$area_id_inside){
						continue;
					}
					$upd = "update pft_roundseat set zone_id=$area_id_inside,custom_num='$custom_num',seat_status={$seat['status']},col_id={$seat['col_id']},row_id={$seat['row_id']} where id=$seat_id limit 1";
					// echo $upd;
					Db::execute($upd); continue;
				}else{// 新增
					$ins  .= "($venue_id,$area_id_inside,'$custom_num',{$seat['col_num']},{$seat['row_num']},{$seat['col_id']},{$seat['row_id']},{$seat['status']}),";
					$ins_f = true;
				}
			}
		}
		
		// 更新为删除
		$seat_del_id = array_filter($seat_del_id );
		if(count($seat_del_id)){
			$upd = "update pft_roundseat set seat_status=1 where venue_id=$venue_id and id not in(".implode($seat_del_id, ',').")";
			Db::execute($upd);
		}
		$ins = rtrim($ins, ',');
		if($ins_f) Db::execute($ins);		
		exit('{"status":"success","msg":"保存成功"}');
		break;
	// 删除场馆
	case 'remove_venue':
		$venue_id = $_REQUEST['venue_id']+0;
		$showCls = new PFT\Show\PftShow(Db::Connect());
		$result  = $showCls->UpdateVenues($venue_id,1);
		if($result['status']=='ok') exit('{"status":"success","msg":"删除成功"}');
		exit('{"status":"fail","msg":"删除失败"}');
		break;
	// 获取场馆列表
	case 'get_venue_list':
		// $showCls = new PFT\Show\PftShow(Db::Connect());
		// $result  = $showCls->GetVenuesName($_SESSION['sid']);
		$venues = array();
		$sql = "SELECT id as venue_id,venue_name,venue_thumb FROM pft_venues WHERE apply_did=? AND status=?";
        $stmt= Db::Connect()->prepare($sql);
        $stmt->execute(array($_SESSION['sid'], 0));
        while($row=$stmt->fetch(\PDO::FETCH_ASSOC)) $venues[] = $row;
		$response = array('status' => 'success', 'data' => $venues);
		exit(json_encode($response));
		break;
	// 获取场馆信息
	case 'get_venue_seat':
		$venue_id = $_REQUEST['venue_id']+0;
		if($venue_id==0) exit('{"status":"fail","msg":"参数错误"}');
		$var['venue_id']  = $venue_id;
		$var['venue_name'] = '';
		$var['col_num']  = '';
		$var['row_num']  = '';
		$var['layout'] = '';
		$var['areas'] = array();
		// 获取场馆信息
		$sql = "select id as venue_id,venue_name,col_num,row_num,layout,col_side as row_head,col_head from pft_venues where id=$venue_id and apply_did={$_SESSION['sid']} limit 1";
		$dbRow = Db::fetch($sql);
		if(!$dbRow) exit('{"status":"fail","msg":"场馆不存在"}');
		
		if(is_array($dbRow)) $var = array_merge($var, $dbRow);
		
		// 获取分区
		$sql = "select id as zone_id,zone_name from pft_roundzone where venue_id=$venue_id";
		$dbRow = Db::fetchAll($sql);

		if(is_array($dbRow) && count($dbRow)) {
			
			foreach($dbRow as $row) $var['areas'][$row['zone_id']] = $row;
			foreach($var['areas'] as $zone_id=>$row){// 获取座位
				$sql = "select id,col_num,row_num,custom_num as seat,seat_status as status,col_id,row_id from pft_roundseat where venue_id=$venue_id and zone_id=$zone_id and seat_status<>1";
				$var['areas'][$zone_id]['seats'] = Db::fetchAll($sql);
			}
		}
		exit(json_encode($var));
		break;
    default:break;
	
}

// 数据转义
function _addslashes($data){
	if(is_array($data)){
		foreach($data as $key=>$value){
			$data[$key] = _addslashes($value);
		}
		return $data;
	}
	return addslashes($data);
}

// 生成语句
function _insertSqlBuild($data, $table){
	if(is_null($data)) return '';
	$sql = 'insert into `'.$table.'` set ';
	foreach($data as $field=>$value){
		$sql = $sql.'`'.$field.'`="'.$value.'",';
	}
	return rtrim($sql,',');
}
// 生成编辑语句
function _updateSqlBuild($data, $table){
	if(is_null($data)) return '';
	$sql = 'update `'.$table.'` set ';
	foreach($data as $field=>$value){
		$sql = $sql.'`'.$field.'`="'.$value.'",';
	}
	$sql = rtrim($sql,',');
	return $sql;
}
?>